package com.baibei.order.bill.querymoney;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.baibei.ebec.sdk.ApiConstant;
import com.baibei.model.PaymentRecordInfo;
import com.baibei.module.LazyLoadFragment;
import com.baibei.module.LoadMoreAdapter;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.order.bill.SimpleRefreshListener;
import com.baibei.order.bill.sticky_header.StickyHeaderScrollListener;
import com.baibei.widget.CommonRefreshLayout;
import com.blankj.utilcode.utils.LogUtils;

import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 收支记录碎片
 */
public class PaymentPickUpRecordFragment extends LazyLoadFragment implements PaymentPickUpRecordContract.PaymentPickUpRecordView, LoadMoreAdapter.OnLoadListener {
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.tv_date)
    TextView mTvDate;
    PaymentPickUpRecordAdapter mAdapter;
    private LoadMoreAdapter mLoadMoreAdapter;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout mRefreshLayout;

    private PaymentPickUpRecordContract.Presenter mPresenter;

    public PaymentPickUpRecordFragment() {
    }

    public static PaymentPickUpRecordFragment newInstance() {
        PaymentPickUpRecordFragment fragment = new PaymentPickUpRecordFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = inject(PaymentPickUpRecordContract.Presenter.class);
    }


    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_payment_pick_up_record;
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new PaymentPickUpRecordAdapter();
        mLoadMoreAdapter = new LoadMoreAdapter(getContext(), mAdapter);

        mRecyclerView.setAdapter(mLoadMoreAdapter);
        mRecyclerView.addOnScrollListener(new StickyHeaderScrollListener(mTvDate));
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (mAdapter.getDataCount() != 0 && mTvDate.getVisibility() == View.GONE) {
                    mTvDate.setVisibility(View.VISIBLE);
                }
            }
        });
        mLoadMoreAdapter.setOnLoadListener(this);

        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPresenter.start();
            }
        });
        mTvDate.setVisibility(View.GONE);
        mRefreshLayout.addPtrUIHandler(new SimpleRefreshListener() {
            @Override
            public void onUIRefreshPrepare(PtrFrameLayout ptrFrameLayout) {
                super.onUIRefreshPrepare(ptrFrameLayout);
                mTvDate.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void lazyLoad() {
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void hideRefreshLayout() {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.refreshComplete();
        }
    }

    @Override
    public void onNoMoreData() {
        mLoadMoreAdapter.hideLoadMoreView();
    }

    @Override
    public void onEmptyData(String s) {
        hideRefreshLayout();
        mLoadMoreAdapter.hideLoadMoreView();
    }

    @Override
    public void onLoadData(List<PaymentRecordInfo> list) {
//        LogUtils.e("tags",list);
        mAdapter.setInfos(list);
        if (list.size() < ApiConstant.PAGE_SIZE) {
            mLoadMoreAdapter.hideLoadMoreView();
        } else {
            mLoadMoreAdapter.showLoadMore();
        }
    }

    @Override
    public void onLoginExpired() {

    }

    @Override
    public void onRetry() {
        mPresenter.start();
    }

    @Override
    public void onLoadMore() {
        mPresenter.loadMore();
    }
}
